package com.dcy.znoa.controller.attendance;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.DateTimeUtil;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.attendance.model.attRecord.PunchModel;
import com.dcy.znoa.controller.publiz.model.oper.SelectOperModel;
import com.dcy.znoa.domain.attendance.AttRecord;
import com.dcy.znoa.service.attendance.AttRecordService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import static com.dcy.znoa.common.util.pojo.JsonCommon.SUCCESS_CODE;

/**
 * 员工考勤原始记录表
 * @author pengyu
 */
@Controller
@RequestMapping("attRecord")
public class AttRecordController extends BaseController {
    @Autowired
    AttRecordService attRecordService;

    //WEB
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/query")
    public JsonForList query(@Valid SelectOperModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        List list = attRecordService.queryListMapper(model);
        return new JsonForList(list);
    }

    //APP  上班打卡，包括上班卡更新    下班打卡，包括下班卡更新
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/punch")
    public JsonCommon punchIn(@Valid PunchModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        //时间格式的转换
//        String recordTimeStr = model.getRecordTimeStr();
//        Date recordTime = DateTimeUtil.getSpecifiedFormatDate(recordTimeStr);
//        model.setRecordTime(recordTime);

        model.setCompId(getCurrentUser().getCompId());
        if(model.getOperId()==null){
            model.setOperId(getCurrentUserId());
        }
//        Date attTime = DateTimeUtil.getYMRDate(recordTimeStr);
        Date now = new Date();
        model.setRecordTime(now);
        model.setAttTime(now);
        attRecordService.punch(model);
        return new JsonCommon(SUCCESS_CODE);
    }




}
